<?php


namespace search\controllers\bigdata;


use business\elasticsearch\services\BigDataProjectService;

class ProjectController extends BaseController
{
    public $enableCsrfValidation = false;//csrf安全

    /**
     * @api {get} /bigdata/project/get-list 01、大数据项目搜索
     *
     * @apiVersion 1.0.0
     * @apiGroup bigdataTakenLand
     *
     * @apiDescription 01、大数据项目搜索
     *
     * @apiParam {String} keyword                           搜索词
     * @apiParam {String} province                          省份
     * @apiParam {String} city                              城市
     * @apiParam {String} company_type                      企业类型
     * @apiParam {String} fitment_fee_label                 装修状态
     * @apiParam {String} property_category_label           项目类型
     * @apiParam {String} opening_time_start/opening_time_end   开盘日期
     * @apiParam {String} delivery_time_start/delivery_time_end 交房日期
     * @apiParam {String} is_price_gt_avg                   高端项目标签
     * @apiParam {String} page                              页码
     * @apiParam {String} pageSize                          分页size
     *
     * @apiSuccess {Boolean} success                    返回状态
     * @apiSuccess {String}  message                    返回消息内容
     * @apiSuccess {Number}  errorCode                  错误码
     * @apiSuccess {Object}  data                       对象数组
     *
     */
    public function actionGetList()
    {
        $keyword = $this->getParam('keyword');
        $province = $this->getParam('province_code');
        $city = $this->getParam('city_code');
        $companyType = $this->getParam('company_type');//企业类型
        $companyArea = $this->getParam('company_area');//企业地区
        $fitmentFeeLabel = $this->getParam('fitment_fee_label');//装修类型
        $propertyCategoryLabel = $this->getParam('property_category_label');//项目类型
        $openingTimeStart = $this->getParam('opening_time_start');//开盘时间
        $openingTimeEnd = $this->getParam('opening_time_end');//开盘时间
        $deliveryTimeStart = $this->getParam('delivery_time_start');//交房时间
        $deliveryTimeEnd = $this->getParam('delivery_time_end');//交房时间
        $isPriceGtAvg = $this->getParam('is_price_gt_avg');
        $area = $this->getParam('area');//项目地区
        $createdTime = $this->getParam('created_time');
        $organizingCode = $this->getParam('organizing_code');
        $page = $this->getParam('page');
        $pageSize = $this->getParam('page_size');
        $page = intval($page) > 0 ? intval($page) : 1;
        $pageSize = intval($pageSize) > 0 ? intval($pageSize) : 10;

        $province = array_filter(explode(",", $province));
        $city = array_filter(explode(",", $city));
        $area = array_filter(explode(",", $area));
        $companyArea = array_filter(explode(",", $companyArea));
        $companyType = array_filter(explode(",", $companyType));
        $fitmentFeeLabel = array_filter(explode(",", $fitmentFeeLabel));
        $isPriceGtAvg = array_filter(explode(",", $isPriceGtAvg));
        $keyword = array_filter(explode(",", $keyword));
        $propertyCategoryLabel = array_filter(explode(",", $propertyCategoryLabel));
        $organizingCode = array_filter(explode(",", $organizingCode));

        //企业标签
        $tags = [];
        $developer_top_100_label = $this->getParam('developer_top_100_label');
        if (!empty($developer_top_100_label)) {
            $tags['developer_top_100_label'] = $developer_top_100_label;
        }
        $newDecorateExpandLabel = $this->getParam('new_decorate_expand_label');
        if (!empty($newDecorateExpandLabel)) {
            $tags['new_decorate_expand_label'] = $newDecorateExpandLabel;
        }
        $newLandUsageExpandLabel = $this->getParam('new_land_usage_expand_label');
        if (!empty($newLandUsageExpandLabel)) {
            $tags['new_land_usage_expand_label'] = $newLandUsageExpandLabel;
        }
        $newAreaExpandLabel = $this->getParam('new_area_expand_label');
        if (!empty($newAreaExpandLabel)) {
            $tags['new_area_expand_label'] = $newAreaExpandLabel;
        }
        $priceGtAvgLabel = $this->getParam('price_gt_avg_label');
        if (!empty($priceGtAvgLabel)) {
            $tags['price_gt_avg_label'] = $priceGtAvgLabel;
        }
        $urbanInvestmentLabel = $this->getParam('urban_investment_label');
        if (!empty($urbanInvestmentLabel)) {
            $tags['urban_investment_label'] = $urbanInvestmentLabel;
        }

        try {
            $data = (new BigDataProjectService())->getDataList(
                $keyword,
                $province,
                $city,
                $area,
                $companyArea,
                $companyType,
                $fitmentFeeLabel,
                $propertyCategoryLabel,
                $openingTimeStart,
                $openingTimeEnd,
                $deliveryTimeStart,
                $deliveryTimeEnd,
                $isPriceGtAvg,
                $createdTime,
                $tags,
                $organizingCode,
                $page,
                $pageSize
            );

            return $this->success($data);
        } catch (\Exception $exception) {
            \Yii::error('找项目搜索失败:' . $exception->getMessage());
            return $this->error(-1, $exception->getMessage());
        }
    }

    public function actionGuessNames()
    {
        try {
            $keyword = $this->getParam('keyword');
            $pageSize = $this->getParam('page_size');
            $pageSize = intval($pageSize) > 0 ? intval($pageSize) : 10;
            $data = (new BigDataProjectService())->guessNames($keyword, $pageSize);
            return $this->success($data);
        } catch (\Exception $exception) {
            \Yii::error('找项目名称联想失败:' . $exception->getMessage());
            return $this->error(-1, $exception->getMessage());
        }
    }
}